package kt.com.bilin.coin.ui.fragment

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import com.bilin.coin.ApiConstant
import com.bilin.coin.App
import com.bilin.coin.R
import com.bilin.coin.databinding.ItemWelfareActivityZoneBinding
import com.bumptech.glide.Glide
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.umeng.socialize.PlatformConfig
import com.umeng.socialize.bean.SHARE_MEDIA
import kotlinx.android.synthetic.main.fragment_v2_welfare.*
import kt.com.bilin.coin.api.API_SERVICE
import kt.com.bilin.coin.api.ApiObserver
import kt.com.bilin.coin.api.toData
import kt.com.bilin.coin.bean.WelfareActivityZoneBean
import kt.com.bilin.coin.common.*
import kt.com.bilin.coin.ui.activity.ImageShowActivity
import kt.com.bilin.coin.ui.activity.MainActivity
import kt.com.bilin.coin.ui.adapter.BaseRecyclerAdapter
import kt.com.bilin.coin.ui.adapter.BaseViewHolder
import kt.com.bilin.coin.ui.dialog.WxReadDialog
import kt.com.bilin.coin.utils.BLUtils
import kt.com.bilin.coin.utils.FullyGridLayoutManager

class WelfareFragment : BaseFragment(R.layout.fragment_v2_welfare) {
    private val mWelfareActivityZoneAdapter: WelfareActivityZoneAdapter = WelfareActivityZoneAdapter()
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        refreshLayout.setEnableLoadMore(false)
        refreshLayout.setEnableRefresh(false)
        bindViewClick(this, ivLuckyDraw, buttonToFinishArticle, buttonToFinishInvitationRealName, buttonToFinishVideo)
        mWelfareActivityZoneAdapter.datas = mutableListOf<WelfareActivityZoneBean>().apply {
            add(WelfareActivityZoneBean(R.mipmap.bg_welfare_new, R.string.text_welfare_new.getString(), R.string.text_welfare_new_desc.getString()))
            add(WelfareActivityZoneBean(R.mipmap.bg_welfare_invitation_reward, R.string.text_welfare_invitation_reward.getString(), R.string.text_welfare_invitation_reward_desc.getString()))
            add(WelfareActivityZoneBean(R.mipmap.bg_welfare_new_activity, R.string.text_welfare_new_activity.getString(), R.string.text_welfare_new_activity_desc.getString()))
            add(WelfareActivityZoneBean(R.mipmap.bg_welfare_life, R.string.text_welfare_life.getString(), R.string.text_welfare_life_desc.getString()))
        }
        mWelfareActivityZoneAdapter.setOnClickAdapterListenter(::onClick)
        rvActivityZone.layoutManager = FullyGridLayoutManager(requireContext(), 2)
        rvActivityZone.adapter = mWelfareActivityZoneAdapter
        Glide.with(this).asGif().load(R.mipmap.gif_lucky_draw).into(ivLuckyDraw)
    }

    override fun onClick(v: View?) {
        when (v) {
            ivLuckyDraw -> ARouterNavigation(PATH_LUCKY_TURNTABLE)
            buttonToFinishArticle,
            buttonToFinishVideo,
            buttonToFinishInvitationRealName,
            -> ARouterNavigation(PATH_TASK)
        }
    }

    private fun onClick(v: View, position: Int) {
        val mWelfareActivityZoneBean = mWelfareActivityZoneAdapter.datas[position]
        when (mWelfareActivityZoneBean.icon) {
            R.mipmap.bg_welfare_new -> ARouterNavigation(PATH_IMAGE_SHOW) {
                withString(ImageShowActivity.TITLE, mWelfareActivityZoneBean.title)
                withInt(ImageShowActivity.IMAGE_RES_ID, R.mipmap.image_welfare_new)
            }
            R.mipmap.bg_welfare_new_activity -> ARouterNavigation(PATH_IMAGE_SHOW) {
                withString(ImageShowActivity.TITLE, mWelfareActivityZoneBean.title)
                withInt(ImageShowActivity.IMAGE_RES_ID, R.mipmap.image_welfare_new_activity)
            }
            R.mipmap.bg_welfare_invitation_reward -> ARouterNavigation(PATH_IMAGE_SHOW) {
                withString(ImageShowActivity.TITLE, mWelfareActivityZoneBean.title)
                withInt(ImageShowActivity.IMAGE_RES_ID, R.mipmap.image_welfare_invitation_reward)
            }
            R.mipmap.bg_welfare_life -> (activity as MainActivity).changeTab(0)
        }
    }

    inner class WelfareActivityZoneAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemWelfareActivityZoneBinding>, WelfareActivityZoneBean>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemWelfareActivityZoneBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<ItemWelfareActivityZoneBinding>, bean: WelfareActivityZoneBean, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    ivActivityZone.setImageResource(icon)
                    tvActivityZoneTitle.text = title
                    tvActivityZoneDesc.text = desc
                    setViewClick(position, root)
                }
            }
        }
    }
}